local idle_text = {}
local idle_max = 100
for _ = 1, idle_max do
    local panel = class.panel.create('',0,0,200,32)
    panel.icon = panel:add_panel('',-32,0,32,32)
    panel.text = class.text:builder {
        parent = panel,
        text = '',
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow_color = 0xff000000,
        outline = true,
        show_flag = true,
		font_size = 20,
        align = 5,
        font = 'damage.ttf',
    }
    table.insert(idle_text, panel)
end


local texttag = {}
ac.ui_tag = texttag
setmetatable(texttag, texttag)
texttag.group = {}

-- 结构
local mt = {}
texttag.__index = mt

-- 类型
mt.type = 'ui tag'

-- 句柄
mt.handle = nil

-- 文本内容
mt.string = '无文本'

-- 缩放
mt.scale = 1
mt.font_size = 12

-- 初始位置
mt.position = ac.point(0, 0)

-- 速度
mt.speed = 0

-- 角度
mt.angle = 90

-- 颜色
mt.color = 0xffffffff
mt.alpha = 1

mt.clock = 0

-- 生命周期
mt.life = 3

-- 淡化
mt.fade = 2

-- 可见性
mt.show = true

-- 设置文本
function mt:set_text(string)
	if string then
		self.string = string
    end

    if self.handle then
        self.handle.text:set_text(string or self.string)
        local w = self.handle.text:get_width()
        self.handle.icon:set_position((self.handle.w - w - 32)/2,self.handle.icon.y)
    end
end

-- 设置大小
function mt:set_scale(scale)
	if scale then
		self.scale = scale
    end
	self.handle:set_relative_size(scale or self.scale)
end

function mt:add_scale(diff)
	self.scale = self.scale + diff
	self.handle:set_relative_size(self.scale)
end

-- 设置位置
mt.offset_x = 0
mt.offset_y = 0
function mt:set_position(position)
	if not self.show then
		return
	end
	if position then
		self.target = position
	end
    local point = self.target:get_point()
	local x, y, z = point:get()
	
    local sx, sy = ac.frame.w2s(x, y, z + point:get_height())

    if sx == nil or sx < 0 or sy < 32 or sx > 1920 or sy > 768 then
        self.handle:hide()
    else
        local w = self.handle:get_width()
        self.handle:set_real_position(sx - w/2 + self.offset_x,sy - self.offset_y)
        self.handle:show()
    end
end

-- 设置颜色
function mt:set_color(color)
    if self.handle then
        self.handle.text:set_color(color or self.color)
    end
end

-- 透明度
function mt:set_alpha(alpha)
    if self.handle then
	    self.handle:set_alpha(alpha or self.alpha)
    end
end

-- 创建漂浮文字
function texttag:__call(texttag)
    setmetatable(texttag, self)
    
    local handle = table.remove(idle_text,1)
    if not handle then
        texttag.removed = true
        return texttag
	end
	
	local level = idle_max - #idle_text
    handle:hide()
    handle:set_level(level)
    handle.icon:set_normal_image(texttag.icon or '')
    texttag.init_size = texttag.font_size/20
    texttag.handle = handle
	texttag.handle:set_alpha(1)
    --handle:set_level(idle_max - #idle_text)
	texttag:set_text()
	texttag:set_scale(texttag.init_size)
	texttag:set_position()
	texttag:set_color()
    
	
    self.group[texttag] = true
	
	return texttag
end

-- 移除漂浮文字
function mt:remove()
	if self.removed then
		return
    end
    
	self.removed = true
    
	self.handle:hide()
	self.handle:unbind_world()
	
	table.insert(idle_text, self.handle)
	
	texttag.group[self] = nil
	
    --重新对图层排序
	local level = self.handle.level
    for tt in pairs(texttag.group) do
        if tt.handle.level>level then
            tt.handle:set_level(tt.handle.level - 1)
        end
    end
	
end

mt.angle = 90
mt.speed = 0
function mt:move(angle,speed)
    self.angle = angle or self.angle
    self.speed = speed or self.speed
end

mt.jump_rate = 0.5
mt.jump_height = 25
function mt:update(delta)
	self.clock = self.clock + delta
	if self.clock > self.life - self.fade then
		if self.clock > self.life  then
			self:remove()
			return
		else
			self.alpha = math.max(self.alpha - self.fade * delta,0)
			self:set_alpha()
		end
	end
    if self.life>0 and self.jump_time then
        local time = self.clock
        if time<=self.jump_time then
            local rate = math.sin(time/self.jump_time * 180)
            local size = rate * self.jump_rate
            self.init_size = self.init_size or self.scale
            self:set_scale(self.init_size * (1 + size))
        else
            -- local size = (time-self.jump_time)/(self.life - self.jump_time)
            -- self.init_size = self.init_size or self.scale
            -- self:set_scale(self.init_size * (1 - size*0.5))
        end
    end
    if self.speed~=0 then
        self.offset_x = self.offset_x + delta*self.speed * math.cos(self.angle)
        self.offset_y = self.offset_y + delta*self.speed * math.sin(self.angle)
    end
    -- local x,y,z = self.position:get()
	-- if self.jump > 0 then
	-- 	if self.clock > self.jump then
	-- 		y = y - self.speed / (delta * 1000) / 10
	-- 	else
	-- 		y = y + self.speed / (delta * 1000)
	-- 	end
	-- 	local value = self.jump_max * math.sin(math.max(self.clock,0) / self.life * 180) + self.jump_min
    --     self:set_scale(value)
	-- 	x = x + self.x_speed / (delta * 1000)
	-- else
	-- 	x = x + self.speed * math.cos(self.angle)*delta
	-- 	y = y + self.speed * math.sin(self.angle)*delta
    --     self:set_scale()
	-- end
	-- self.position = ac.point(x,y)
	
	self:set_position()
end


ac.hardware:on("界面绘制")(function(_,delta)
    for tt in pairs(texttag.group) do
        tt:update(delta)
    end
end)

return texttag

